<template>
  <div class="list-table">
    <div v-show="!menuKeyMark" class="area-header">
      <span class="area-header-title">{{ $t('项目费用') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('申请标题')" prop="actTitle">
        <el-input v-model="queryParams.actTitle" :placeholder="$t('申请标题')" keyup.enter.native="queryHandle" @keyup.enter.native="queryHandle" />
      </el-form-item>
      <el-form-item :label="$t('申请人')" prop="userName">
        <el-input v-model="queryParams.userName" :placeholder="$t('申请人')" @keyup.enter.native="queryHandle" />
      </el-form-item>
      <el-form-item v-show="menuKeyMark" :label="$t('是否已维护')" prop="whetherToMaintain">
        <dictSelect
          v-model="queryParams.whetherToMaintain"
          :clearable="true"
          dictionary="payment_maintenance"
          :placeholder="$t('请选择')"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
      </el-form-item>
    </el-form>

    <el-row v-if="!menuKeyMark" :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['deal:expense:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t("新增") }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['deal:expense:export']"
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="exportHandle"
        >
          {{ $t("导出") }}
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" tooltip-effect="darkTable" :data="dealExpenseList" :show-summary="true" :summary-method="getSummaries" border>
      <el-table-column :label="$t('序号')" type="index" align="center" width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('申请标题')" align="center" prop="actTitle" show-overflow-tooltip>
        <template slot-scope="scope">
          <span class="link-type" @click="updateHandle(scope.row)">{{ scope.row.actTitle }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('申请人')" align="center" prop="userName" show-overflow-tooltip width="110" />
      <el-table-column :label="$t('申请时间')" align="center" prop="applyTime" show-overflow-tooltip width="110">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applyTime) }}</span>
        </template>
      </el-table-column>
      <!--<el-table-column label="部门" align="center" prop="departmentName" />-->
      <el-table-column :label="$t('费用金额(元)')" header-align="center" align="right" prop="amountMoney" width="120">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.amountMoney) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('备注')" align="center" prop="costsThat" show-overflow-tooltip />
      <el-table-column :label="$t('审批状态')" align="center" width="120">
        <template slot-scope="scope">
          <ProcessStatus :status="scope.row.actStatus" :act-id="scope.row.actId" />
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" fixed="right" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <div v-if="!menuKeyMark">
            <!-- 启动流程，终止流程，修改，删除 四个按钮 -->
            <ListBtn :bus-data="scope.row" has-permi="deal:expense" />
            <div v-if="scope.row.actStatus === 'W' && (scope.row.payCompletionTime === null || scope.row.payCompletionTime === undefined || scope.row.payCompletionTime === '')">
              <span slot="label">
                <el-button
                  v-hasPermi="['deal:payment:edit']"
                  size="small"
                  type="text"
                  icon="el-icon-edit"
                  @click="updatePayTimeBtn(scope.row)"
                >{{ $t('维护付款信息') }}</el-button>
                <el-tooltip v-hasPermi="['deal:payment:edit']" placement="top">
                  <div slot="content">
                    <span v-html="$t('审批结束后,对费用付款时间的梳理, <br> 这里维护实际到账时间.')" />
                  </div>
                  <i class="el-icon-question" />
                </el-tooltip>
              </span>
            </div>
          </div>
          <div v-else>
            <div v-if="scope.row.actStatus === 'W' && (scope.row.payCompletionTime === null || scope.row.payCompletionTime === undefined || scope.row.payCompletionTime === '')">
              <span slot="label">
                <el-button
                  v-hasPermi="['deal:payment:edit']"
                  size="small"
                  type="text"
                  icon="el-icon-edit"
                  @click="updatePayTimeBtn(scope.row)"
                >{{ $t('维护付款信息') }}</el-button>
                <el-tooltip v-hasPermi="['deal:payment:edit']" placement="top">
                  <div slot="content">
                    <span v-html="$t('审批结束后,对费用付款时间的梳理, <br> 这里维护实际到账时间.')" />
                  </div>
                  <i class="el-icon-question" />
                </el-tooltip>
              </span>
            </div>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <div>
      <el-dialog :title="$t('维护付款时间')" :visible.sync="dialogVisiblePayTime" width="30%" :close-on-click-modal="false" :lock-scroll="false" append-to-body>
        <DatePicker v-model="dialogVisiblePayTimeValue" :placeholder="$t('维护后将不能修改,请确认后操作!')" />
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisiblePayTime = false">{{ $t("取消") }}</el-button>
          <el-button type="primary" @click="updatePayTimeSuccess">{{ $t("确定") }}</el-button>
        </span>
      </el-dialog>
    </div>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改项目费用对话框 -->
    <el-dialog :title="$t('项目费用')" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <dealExpenseForm ref="dealExpenseForm" />
    </el-dialog>
  </div>
</template>

<script>
import {
  listDealExpense,
  listDealExpenseByResult,
  delDealExpense,
  updatePayTime, getDealExpenseSum, getDealExpenseSumByResult
} from '@/api/deal/expense/expense'
import dealExpenseForm from './dealExpenseForm.vue'
import ProcessStatus from '@/components/Act/ProcessStatus'

export default {
  components: {
    dealExpenseForm,
    ProcessStatus
    // Treeselect
  },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 项目费用表格数据
          dealExpenseList: [],
          dealExpenseSum: {},
          // 是否显示弹出层
          formOpen: false,
          // 查询参数
          queryParams: {
              userName: undefined,
              dealId: this.$route.query.id,
              department: undefined,
              costsThat: undefined,
              whetherToMaintain: undefined,
              pageNum: 1,
              pageSize: 10
          },
          // 表单参数
          form: {},
          // 表单校验
          rules: {
          },
          // 部门
          deptOptions: [],
        // 遮盖层
        dialogVisiblePayTime: false,
        // 选中的时间
        dialogVisiblePayTimeValue: new Date(),
        // 要修改的ID
        dialogVisiblePayTimeId: '',
        menuKeyMark: undefined
      }
  },
  created() {
      // this.getTreeselect()
      this.getList()
  },
  methods: {
    /** 查询部门下拉结构 */
    // getTreeselect() {
    //   treeselect().then(response => {
    //     this.deptOptions = response.data
    //   })
    // },
    /** 查询项目费用列表 */
    getList() {
      this.menuKeyMark = this.$route.params.menuKeyMark
      this.loading = true
      if (this.menuKeyMark) {
        listDealExpenseByResult(this.queryParams).then(response => {
          getDealExpenseSumByResult(this.queryParams).then(res => {
            this.dealExpenseSum = res.data
          })
          this.dealExpenseList = response.rows
          this.total = response.total
          this.loading = false
        })
      } else {
        listDealExpense(this.queryParams).then(response => {
          getDealExpenseSum(this.queryParams).then(res => {
            this.dealExpenseSum = res.data
          })
          this.dealExpenseList = response.rows
          this.total = response.total
          this.loading = false
        })
      }
    },
    // 付款时间维护 : 按钮获取数据
    updatePayTimeBtn(row) {
      this.updatePayTime(row)
    },
    // 付款时间维护 : 将数据传递到打开弹窗
    updatePayTime(row) {
      this.dialogVisiblePayTime = true
      this.dialogVisiblePayTimeId = row.id
    },
    // 付款时间维护 : 保存到后台
    updatePayTimeSuccess() {
      try {
        if (this.dialogVisiblePayTimeValue === '' || this.dialogVisiblePayTimeValue === undefined || this.dialogVisiblePayTimeValue === null) {
          this.msgError(this.$t('时间为空!'))
        } else {
          updatePayTime(this.dialogVisiblePayTimeId, new Date(this.dialogVisiblePayTimeValue)).then(() => {
            this.getList()
          })
          this.dialogVisiblePayTime = false
        }
      } catch (e) {
        this.msgSuccess(this.$t('维护失败'))
        this.dialogVisiblePayTime = false
      }
    },
    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            userName: undefined,
            applyTime: undefined,
            department: undefined,
            amountMoney: undefined,
            costsThat: undefined,
            actId: undefined,
            actStatus: undefined,
            remark: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined,
            delFlag: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.$nextTick(() => {
            this.$refs.dealExpenseForm.init(null)
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.dealExpenseForm.init(row)
        })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return delDealExpense(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(function() {
        })
    },
    /** 导出按钮操作 */
    exportHandle() {
        this.downLoadExcel('/dealExpense/dealExpense/export', this.queryParams)
    },
    // 合计
    getSummaries(param) {
      const { columns } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计'
        } else if (index === 4) {
          sums[index] = this.moneyFormat(this.dealExpenseSum.amountMoney)
        } else {
          sums[index] = ''
        }
      })
      return sums
    }
  }
}
</script>
